package top.likeqc.blog.web.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import top.likeqc.blog.pojo.Blog;
import top.likeqc.blog.service.BlogService;
import top.likeqc.blog.service.TypeService;
import top.likeqc.blog.vo.BlogQuery;

/**
 * @author likeqc.top
 * @date 2021/4/16 15:52
 */
@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired private BlogService blogService;

    @Autowired private TypeService typeService;

    @GetMapping("/blogs")
    public String blogs(
            @PageableDefault(
                            size = 3,
                            sort = {"updateTime"},
                            direction = Sort.Direction.DESC)
                    Pageable pageable,
            BlogQuery blog,
            Model model) {
        model.addAttribute("types", typeService.listType());
        model.addAttribute("page", blogService.listBlog(pageable, blog));
        return "admin/blogs";
    }

    @PostMapping("/blogs/search")
    public String search(
            @PageableDefault(
                    size = 3,
                    sort = {"updateTime"},
                    direction = Sort.Direction.DESC)
                    Pageable pageable,
            BlogQuery blog,
            Model model) {
        System.out.println("查询博客");
        model.addAttribute("page", blogService.listBlog(pageable, blog));
        return "admin/blogs :: blogList";
    }
}
